"""三乐购物系统"""

import json

# 商品信息
goods = {"F01": {"name": "apple", "price": 1},
         "F02": {"name": "banana", "price": 2},
         "F03": {"name": "pear", "price": 3}}

# 存储用户信息
userinfo = {"vip": {"passwd": "123456", "money": 10}}
# 账户金额
# money = 0
# 结算金额
# fi_money = 0
#账户余额
# balance = 0
# 购物车
scar = {}

# 登录系统
def login():
    global username, passwd, money
    username = input("请输入用户名：")
    # 可随时退出
    if username == "exit":
        exit()
    else:
        passwd = input("请输入密码：")
        if username in userinfo.keys() and passwd == userinfo[username]["passwd"]:
            print("登录成功！")
            print(f"欢迎用户{username},您的余额为：{userinfo[username]['money']}")
            # balance = userinfo[username]['money']
        else:
            print("用户名或密码错误")
            login()

# 注册账号
def register(money=None):
    new_username = input("请输入新的用户名：")
    # 判断是否有重复的用户名
    if new_username in userinfo.keys():
        print("用户名已存在，请重新输入")
        register()
    elif new_username == "exit":
        exit()
    else:
        new_passwd = input("请设置密码：")
        if new_passwd == "exit":
            exit()
        else:
            new_money = int(input("请输入您的预算金额"))
            if new_money == "exit":
                exit()
            else:
                userinfo[new_username] = {"passwd": new_passwd, "money": new_money}
                print("注册成功，请登录！")
                login()

# 退出后， 打印已购买商品的余额
def exit():
    print("已购商品为：")
    print(scar)
    print(f"您的余额为：{userinfo[username][money]}")
    exit(0)

# 添加购物车
def addgoods():
    choice = input("请输入商品编号：")
    if choice in goods.keys():
        scar[choice] = goods[choice]
    elif choice == "exit":
        exit()
    else:
        print("没有此商品，请重新输入")
        addgoods()

# 查看商品信息和购物车信息
def catgoods():
    print("商品信息：".center(10, "-"))
    for i, j in goods.items():
        print(f"商品编号：{i}, 商品名称：{j['name']}, 商品价格{j['price']}")

def catscar():
    global fi_money
    print("购物车信息：".center(10, "-"))
    for h, k in scar.items():
        if len(scar) == "0":
            print("您暂未加购商品")
        else:
            print(f"商品编号：{h}, 商品名称：{k['name']}, 商品价格{k['price']}")
        fi_money += int(k['price'])
    print(f"您购物车的总金额为：{fi_money}")

# 结算
def pay():
    if fi_money > userinfo[username]['money']:
        print("余额不足，超过您的预算啦！")
    else:
        userinfo[username]['money'] -= fi_money
        print(f"扣款成功，您的余额为{userinfo[username]['money']}")


while 1:
    print("/    欢迎来到三乐购物系统    /".center(50, "*"))
    print("1.注册".center(50, "*"))
    print("2.登录".center(50, "*"))
    print("3.exit".center(51, "*"))
    option = input("请输入你的选择：")
    if option == "1":
        register()
        print("A.查看商品信息")
        print("B.查看购物车信息")
        print("C.添加商品到购物车")
        print("exit")
        option2 = input("A.查看商品信息")
        if option2 == "A":
            catgoods()
        elif option2 == "B":
            catscar()
        elif option2 == "exit":
            exit()
    elif option == "2":
        login()
    elif option == "3":
        exit()

